package org.feidao.chapter91.elasticSearch;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.UUID;

/**
 * Created by linghufeixia on 2023-2-16.
 * Elasticsearc document工具类
 */
@Component
public class DocumentUtil {
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    /**
     * 添加document
     * @param object
     * @param index
     * @param id
     * @return
     */
    public void addDocument(Object object, String index,String id){
        //1 create IndexRequest
        IndexRequest indexRequest = new IndexRequest(index);
        indexRequest.id(id);
        indexRequest.timeout(TimeValue.timeValueSeconds(1));
        String content = JSON.toJSONString(object);
        indexRequest.source(content, XContentType.JSON);
        try {
            //2 send IndexRequest
            IndexResponse indexResponse =restHighLevelClient.index(indexRequest,RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加document,随机id
     * @param object
     * @param index
     * @return
     */
    public void addDocument(Object object, String index){
        String id= "";
        id = UUID.randomUUID().toString().replaceAll("-","").toUpperCase();
        addDocument(object, index, id);
    }

    /**
     * get Document
     * @param index
     * @param id
     * @return
     */
    public GetResponse getDocumnet(String index,String id){
        GetResponse getResponse = null;
        GetRequest getRequest = new GetRequest(index, id);
        try {
            getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
        }catch (IOException e){
            e.printStackTrace();
        } finally {
            return  getResponse;
        }
    }

    /**
     * update document
     * @param object
     * @param index
     * @param id
     */
    public void updateDocument(Object object, String index,String id){
        //1 create UpdateRequest
        UpdateRequest updateRequest = new UpdateRequest(index,id);
        updateRequest.timeout(TimeValue.timeValueSeconds(1));
        String content = JSON.toJSONString(object);
        updateRequest.doc(content, XContentType.JSON);
        try {
            //2 send UpdateRequest
            UpdateResponse updateResponse =restHighLevelClient.update(updateRequest,RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除Document
     * @param index
     * @param id
     */
    public void deleteDocument(String index,String id){
        //1 create DeleteRequest
        DeleteRequest deleteRequest = new DeleteRequest(index, id);
        try {
            //2 send DeleteRequest
            DeleteResponse deleteResponse =  restHighLevelClient.delete(deleteRequest,RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public SearchResponse search(String index, TermQueryBuilder termQueryBuilder){
        //1 create SearchRequest
        SearchRequest searchRequest = new SearchRequest(index);

        //2 create SearchSourceBuilder
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(termQueryBuilder);
        searchSourceBuilder.timeout(TimeValue.timeValueSeconds(60));

        //3 查询条件放入SearchRequest
        searchRequest.source(searchSourceBuilder);

        //4 send SearchRequest
        SearchResponse searchResponse = null;
        try {
            searchResponse = restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return searchResponse;
        }
    }

    public SearchResponse searchHighLight(String index, TermQueryBuilder termQueryBuilder){
        //1 create SearchRequest
        SearchRequest searchRequest = new SearchRequest(index);

        //2 create SearchSourceBuilder
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.highlighter();
        searchSourceBuilder.query(termQueryBuilder);
        searchSourceBuilder.timeout(TimeValue.timeValueSeconds(60));

        //3 查询条件放入SearchRequest
        searchRequest.source(searchSourceBuilder);

        //4 send SearchRequest
        SearchResponse searchResponse = null;
        try {
            searchResponse = restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return searchResponse;
        }
    }
}
